﻿$(function()
 {
    $('input').each(function()
    {
        var default_value = this.value;
        $(this).focus(function()
        {
            if (this.value == default_value) {
                this.value = '';
            }
        });
        $(this).blur(function()
        {
            if (this.value == '') {
                this.value = default_value;
            }
        });
    });
    createCode();
});

function doLogin()
 {
    $(this).addClass("loading");
    if (CheckValue())
    {
        if (valiCode())
        {
            window.location.replace("Default.aspx");
        }
    }
}

function CheckValue()
 {
    var name = $("#loginname").val();
    var pwd = $("#loginpwd").val();

    if (name != "" || pwd != "")
    {
        if (name == "")
        {
            ShowMessage("red", _MSG.accountNotNull);//帳號不能為空
            window.event.returnValue = "";
            return false;
        }
        else if (pwd == "")
        {
            ShowMessage("red", _MSG.passwordNotNull);//"密碼不能為空!");
            window.event.returnValue = "";
            return false;
        }
        rewrite();
        return true;
    }
    else
    {
        ShowMessage("red",  _MSG.accountPasswordNotNull);//"帳號、密碼不能為空 !");
        window.event.returnValue = "";
        return false;
    }

}

function ShowMessage(color, msg)
 {
    $("#msg").css("color", color).html(msg);
}

var code;
function createCode()
 {
    code = "";
    var codeLength = 4;
    var selectChar = new Array(0,1,2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
    for (var i = 0; i < codeLength; i++)
    {
        var charIndex = Math.floor(Math.random() * 36);
        code += selectChar[charIndex];
    }
    $("#checkcode").html(code);
}

function valiCode()
 {
    var inputCode = $("#code").val();
    if (inputCode.length <= 0)
    {
        ShowMessage("red","請輸入驗證碼！");
        window.event.returnValue = "";
        return false;
    }
    else if (inputCode.toUpperCase() != code.toUpperCase())
    {
        ShowMessage("red","驗證碼輸入錯誤！");
        window.event.returnValue = "";
        createCode();
        return false;
    }
    return true;
}

function checkType() {
    var newLoginName = $("#loginname").val().trim();
    if (newLoginName == "") {
        setNeedDynamicPasswd(false);
        $("#pwdcard,#pwdcardP").hide();
        return;
    }
    
    $.ajax({
        type: "post",
        dataType: "text",
        url: "inc/Login.ashx",
        data: "MothodName=CheckPwdCard&loginName=" + newLoginName,
        error: function(xhr, status) {
            alert("檢測過程發生錯誤:" + xhr.responseText + "\n请联络系统管理员！");
            $("#uid").val('');
        },
        success: function(data) {
           if(data.indexOf("<otp>1</otp>")==-1)
           {
              $("#pwdcard,#pwdcardP").hide();
              $("#needcard").val("N");
           }
           else
           {
              $("#pwdcard,#pwdcardP").show();
              $("#needcard").val("Y");
           }
           //cookie("DefaultAuthOf" + newLoginName, defaultAuth);
        }
    });
}